<div id="page-heading">
      <ol class="breadcrumb">
        <li><a href="<?php echo(url(''));?>">Portada</a></li>
        <li>Joc Net</li>
        <li class="active">Crear sorteig</li>
      </ol>
    </div>
    <div class="container">
<div class="row">
     <div class="col-md-12">
                            <!-- Custom Tabs -->
                            <div class="nav-tabs-custom">
                                <ul class="nav nav-tabs">
                                    <li class="active"><a href="#tab_1" data-toggle="tab">Nou Sorteig</a></li>
                                    <li class="pull-right"><a href="#" class="text-muted"><i class="fa fa-gear"></i></a></li>
                                </ul>
                                <div class="tab-content col-md-12">
                                    <div class="tab-pane active col-md-12" id="tab_1">
                                    		<div class="col-md-4">
		                                  	    <h4 class="box-title">Nom Sorteig</h4>
		                                        <div class="input-group col-md-12">
		                                       		 <span class="input-group-addon">Nom sorteig</span>
		                                       	 	 <input type="text" placeholder="Títol" id="nom_sorteig" class="form-control">
		                                   		</div>
												<h4 class="box-title">Texto Descripcion</h4>
												<div class="input-group col-md-12" >
													<textarea id="text_desc" placeholder="Enter ..." rows="3" class="form-control"></textarea>										
												</div>
												<h4 class="box-title">Fins a</h4>
		                                        <div class="input-group col-md-12">
		                                       		 <span class="input-group-addon"></span>
		                                       	 	 <input type="text" placeholder="Fins al ..." id="text_data" class="form-control">
		                                   		</div>
												<h4 class="box-title">Duracio sorteig</h4>
												<div class="input-group col-md-12">
											     	<input type="text" name="reservation" onclick="calendari_nou_sorteig();"  class="form-control col-md-6 " value="" id="calendari_nou_sorteig"/>
											    </div>
												<div class="input-group col-md-12"  style="padding:15px">
				                                        <label>
				                                        	Actiu 
				                                            <input type="checkbox" id="actiu_sorteig" class="flat-red" />
				                                        </label>
												</div>
											</div>
											<div class="col-md-6">
												<h4 class="box-title">Sel·leccionar foto</h4>
			                                   		<div class="input-group col-md-12"  style="padding:15px" >
			                                   			<div class="col-md-12">
			                                   				<div id="imatge_sorteig" class="col-md-12" style="width:200px; height:220px; border-style:dotted; border-width:1px; margin-bottom:2em;"></div>
			                                   			</div>
														<div class="col-md-12">
															<input id="avatar" type="file" class="btn btn-info btn-flat" onchange="cargar_foto();" name="avatar" ></p>
														</div>
													</div>
											</div>
	                                   	 <div class=" col-md-12"></p>
												<button class="btn btn-default pull-left" onclick="crear_sorteo();">
													<i class="fa fa-plus" ></i> Crear Sorteig
												</button>
										</div>		
                                    </div><!-- /.tab-pane -->
                                   

                                </div><!-- /.tab-content -->
                                 
                            </div><!-- nav-tabs-custom -->
                            
                        </div><!-- /.col -->
                       

</div>

</div>


<script>

getTotalSorteosActivos();

$('#dp3').datepicker({ });

function cargar_foto(){

	var file_data = $("#avatar").prop("files")[0]; // Getting the properties of file from file field
	var form_data = new FormData(); // Creating object of FormData class
	form_data.append("file", file_data) // Appending parameter named file with properties of file_field to form_data
	form_data.append("user_id", 123) // Adding extra parameters to form_data
		$.ajax({
		url: url_base+"imatges/uploadImatge",
		dataType: 'script',
		cache: false,
		contentType: false,
		processData: false,
		data: form_data, // Setting the data attribute of ajax with file_data
		type: 'post',
		success: function(imatge) {
			var nom_imatge = imatge;
			nom_imatge = nom_imatge.substring(1, nom_imatge.length-1);

			$("#imatge_sorteig").empty();
			$("#imatge_sorteig").append('<img id="'+nom_imatge+'" src="/jocnet_nova/images/sortejos/'+nom_imatge+'" class="resize_fit_center">');

		}
	});
	
}

function crear_sorteo(){

	var data_actiu = $("#calendari_nou_sorteig").val();
	var nom_sorteig = $("#nom_sorteig").val().replace("'","\\'");;
	var foto_portada = $(".resize_fit_center").attr('id');
	var text_desc = $("#text_desc").val().replace("'","\\'");;
	var text_data = $("#text_data").val().replace("'","\\'");;
	var actiu = $("#actiu_sorteig").attr("checked");

	if(data_actiu == ''){alert('No sha definit durada del curs'); return;}
	if(typeof foto_portada == 'undefined'){ alert('No shacarregat cap imatge'); return;}

	$.ajax({
	    url: url_base+"sortejos/crear_nou",
	    data: "nom_sorteig="+nom_sorteig+"&data_actiu="+data_actiu+"&foto_sorteig="+foto_portada+"&text_desc="+text_desc+"&text_data="+text_data+"&actiu="+actiu,
	    type: "POST",			      
		    beforeSend: function() {
		
		}, 
		success: function(data){
				var data = jQuery.parseJSON(data);
				
				if(data === true){
					alert('Sorteig creat');
					location.reload(true);
				}
	 	}
	});
	
}


function getTotalSorteosActivos(){
	$.ajax({
	    url: url_base+"sortejos/total_sortejos_actius",
	    type: "POST",			      
		    beforeSend: function() {
		    	
		}, 
		success: function(data){
				var obj = jQuery.parseJSON(data);
				
				$.each( obj, function( key, valor ) {
					total_sortejos = valor.total;
					if(total_sortejos >=4){
						alert('Ya hay 4 sorteos activos.');
					}
				}); 
		 	}
	});
}



</script>